﻿using mozhi.smarterp.Finance.Enums;
using System;
using System.Collections.Generic;
using System.Linq;

namespace mozhi.smarterp.Finance.Reports
{
    public class FinancialReportItem : AggregateRootBase<Guid>
    {
        public string Code { private set; get; }

        public string ParentCode { private set; get; }

        public string AssociateCode {  private set; get; }

        public FinancialReportType ReportType { private set; get; }

        public AccountingStandardType AccountingStandard { private set; get; }

        public string Number { private set; get; }

        public string Name {  private set; get; }

        //public FinancialReportItemType ItemType { private set; get; }

        public int Index {  private set; get; }

        public List<FinancialReportItemSetting> Settings { private set; get; }

        private FinancialReportItem() { }

        public FinancialReportItem(Guid id , string code , string parentCode , string associateCode , FinancialReportType reportType , 
            AccountingStandardType accountingStandard , string number , string name ,  int index)
        {
            Id = id;
            Code = code;
            ParentCode = parentCode;
            AssociateCode = associateCode;
            ReportType = reportType;
            AccountingStandard = accountingStandard;
            Number = number;
            Name = name;
            //ItemType = itemType;
            Index = index;
            Settings = new List<FinancialReportItemSetting>();
        }

        public void AddItem(FinancialReportItemSetting reportItemSetting)
        {
            var match = Settings.FirstOrDefault(x=>x.Id == reportItemSetting.Id);

            if (match != null)
                return;

            Settings.Add(reportItemSetting);
        }

        public void ChangeItems(List<FinancialReportItemSetting> settings)
        {
            foreach (var setting in settings)
            {
                var match = Settings.FirstOrDefault(x => x.Id == setting.Id);

                if (match == null)
                    Settings.Add(setting);
                else
                    match.Change(setting.CalculateTarget, setting.Operator, setting.Precondition);
            }

            var settingIds = settings.Select(x => x.Id).ToList();

            var removedSettings = Settings.Where(x => !settingIds.Contains(x.Id)).ToList();

            
            foreach (var setting in removedSettings)
            {
                Settings.Remove(setting);
            }
        }
    }


    public class FinancialReportItemSetting : EntityBase<Guid>
    {
        public Guid FinancialReportItemId { private set; get; }

        public string FinancialReportItemCode { private set; get; }

        /// <summary>
        /// 1 借方余额 2 贷方余额 3借方余额为正 4贷方余额为正 5借方余额为负 6贷方余额为负 9借方发生额  10贷方发生额 11期间净发生额
        /// </summary>
        public int? ValueRule { private set; get; }

        /// <summary>
        /// 计算对象（科目代码或者行号（R开头））
        /// </summary>
        public string CalculateTarget { private set; get; }

        /// <summary>
        /// 操作符号（+ ， -）
        /// </summary>
        public string Operator { private set; get; }

        /// <summary>
        /// 前置条件
        /// </summary>
        public int? Precondition { private set; get; }

        private FinancialReportItemSetting() { }

        public FinancialReportItemSetting(Guid id, Guid reportItemId, string reportItemCode,
            string calculateTarget, string operatorType, int? precondition)
        {
            Id= id;
            FinancialReportItemId = reportItemId;
            FinancialReportItemCode = reportItemCode;
            //ValueRule = valueRule;
            CalculateTarget = calculateTarget;
            Operator = operatorType;
            Precondition = precondition;
        }

        public void Change(string calculateTarget, string operatorType, int? precondition)
        {
            CalculateTarget = calculateTarget;
            Operator = operatorType;
            Precondition = precondition;
        }

    }

}
